package pl.edu.mimuw.irs.rcp.view;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.part.ViewPart;

import pl.edu.mimuw.irs.core.model.design.resource.Roles;
import pl.edu.mimuw.irs.rcp.controller.editor.roles.RolesTableWrapper;
import pl.edu.mimuw.irs.rcp.persistence.PersistenceManager;
import pl.edu.mimuw.irs.rcp.util.Logger;
import pl.edu.mimuw.irs.rcp.util.MessageCode;

/**
 * @author Jakub Rauch
 * Created on: 2009-05-25
 */
public class RolesView extends ViewPart {

	public static final String ID = "pl.edu.mimuw.irs.view.roles";

	// *** Attributes
	
	private RolesTableWrapper table;
	private IWorkbenchPart lastSelection;
	private ISelectionListener listener = new ISelectionListener() {
		@Override
		public void selectionChanged(IWorkbenchPart sourcepart, ISelection selection) {
			/* 
			 * We ignore our own selections and the same elements selection.
			 */
			if (lastSelection == sourcepart) return;
			lastSelection = sourcepart;
			//if (sourcepart != RolesView.this) {
			    showSelection(sourcepart, selection);
			//}
		}

	};
	
	// *** Constructors

	public RolesView() {
		super();
	}

	// *** Methods

	private void showSelection(IWorkbenchPart sourcepart,
			ISelection selection) {
		setContentDescription(sourcepart.getTitle() + " (" + selection.getClass().getName() + ")");
		/*
		 * Get the IProject to access its session properties for Roles
		 */
		if (!(sourcepart instanceof IEditorPart)) return;
		IEditorPart editor = (IEditorPart) sourcepart;
		IEditorInput editorInput = editor.getEditorInput();
		Roles roles = loadRoles(editorInput);
		if (roles == null) return;
		
		/*
		 * Here we have roles.
		 */
		if (table.getRoles() != roles) {
			table.setRoles(roles);
		}
	}
	
	private Roles loadRoles(IEditorInput input) {
		try {
			PersistenceManager manager = PersistenceManager.getInstance(input);
			if (manager == null) return null;
			return manager.getRoles();
		} catch (CoreException e) {
			Logger.handle(MessageCode.ROLES_LOAD_DESERIALIZE_PROBLEM, e, input);
			return null;
		} catch (Exception e) {
			Logger.handle(MessageCode.ROLES_LOAD_DESERIALIZE_PROBLEM, e, input);
			return null;
		}
	}
	
	/**
	 * This is a callback that will allow us to create the viewer and initialize
	 * it.
	 */
	public void createPartControl(Composite parent) {
		table = new RolesTableWrapper(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL, null, null);
		getSite().setSelectionProvider(table.getTableViewer());
		getSite().getWorkbenchWindow().getSelectionService().addSelectionListener(listener);
	}


	/**
	 * Passing the focus request to the viewer's control.
	 */
	public void setFocus() {
		table.getTableViewer().getControl().setFocus();
	}
	
}
